<!--
 * @Description: 
 * @Version: 
 * @Autor: ps
 * @Date: 2022-09-08 11:43:43
 * @LastEditors: ps
 * @LastEditTime: 2022-10-27 16:27:55
-->
<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="120px">
    <el-form-item label="用户昵称" prop="nickName">
      <el-input v-model="user.nickName" maxlength="30" />
    </el-form-item>
    <el-form-item label="手机号码" prop="phonenumber">
      <el-input v-model="user.phonenumber" maxlength="11" />
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="user.email" maxlength="50" />
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-radio-group v-model="user.sex">
        <el-radio label="0">男</el-radio>
        <el-radio label="1">女</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="电子签名" prop="electronicSignatureUrl">
      <div class="electronicSignatureUrl">
        <img
          v-if="user.electronicSignatureUrl"
          :src="baseUrl + user.electronicSignatureUrl"
        />
        <el-empty
          v-else
          style="width: 100%; height: 300px"
          description="暂无签名"
        ></el-empty>
      </div>
      <el-button type="primary" size="small" plain @click="open"
        >上传签名</el-button
      >
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="submit">保存</el-button>
      <el-button type="danger" size="small" @click="close">关闭</el-button>
    </el-form-item>
    <el-dialog
      :visible.sync="codeVisible"
      title="请用手机扫描下方二维码"
      width="500px"
      append-to-body
      :before-close="handleClose"
    >
      <vueQr :text="codeImg" :size="400" :margin="0"></vueQr>
    </el-dialog>
  </el-form>
</template>

<script>
import vueQr from "vue-qr";
import { updateUserProfile } from "@/api/system/user";
import { checkPassword, profileSignature } from "@/api/esign";
export default {
  props: {
    user: {
      type: Object,
    },
  },
  components: {
    vueQr,
  },
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      timer: null,
      codeVisible: false,
      codeImg: "",
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" },
        ],
        email: [
          { required: true, message: "邮箱地址不能为空", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        phonenumber: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  destroy() {
    clearInterval(this.timer);
  },
  methods: {
    open() {
      const that = this;
      that
        .$prompt("请输入密码", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputType: "password",
        })
        .then(({ value }) => {
          checkPassword({
            password: value,
          }).then((res) => {
            if (res.code === 700) {
              that.$message.warning(res.msg);
              return false;
            }
            that.codeVisible = true;
            that.codeImg = window.location.origin + res.data;
            this.btnTimer();
          });
        })
        .catch(() => {});
    },
    btnTimer() {
      const that = this;
      this.timer = setInterval(() => {
        profileSignature().then((res) => {
          if (res.data) {
            that.user.electronicSignatureUrl = res.data;
            this.$message.success("上传成功");
            that.handleClose();
          }
        });
      }, 1000);
    },
    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateUserProfile(this.user).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.clear();
          });
        }
      });
    },
    handleClose() {
      this.codeVisible = false;
      this.clear();
    },
    clear() {
      clearInterval(this.timer);
    },
    close() {
      this.$tab.closePage();
    },
  },
};
</script>
<style scoped lang="scss">
.electronicSignatureUrl {
  width: 100%;
  height: 300px;
  img {
    width: 100%;
    height: 300px;
  }
}
</style>


